package ru.efko.dims.activity;

import java.util.Timer;
import java.util.TimerTask;

import org.achartengine.GraphicalView;

import ru.efko.dims.R;
import ru.efko.dims.chart.SensorValueChart;
import ru.efko.dims.domain.DataChangeHandler;
import ru.efko.dims.domain.SensorDevice;
import ru.efko.dims.domain.SensorDeviceImpl;
import ru.efko.dims.util.ThreadLocalVariablesKeeper;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class GraphicsActivity extends Activity implements DataChangeHandler {

	private GraphicalView mChartView;
	private Button startButton;
	private Button stopButton;
	private static final int MENU_SETTINGS = 1;
	private static final int MENU_DISCONNECT = 2;
	private static final float BUTTON_SIDE = 60; // dp
	private SensorValueChart chart = new SensorValueChart();

	private Timer timer = new Timer();
	private SensorDevice sensorDevice;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.requestWindowFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.activity_main);
		startButton = (Button) findViewById(R.id.startButton);
		stopButton = (Button) findViewById(R.id.stopButton);
		sensorDevice = ThreadLocalVariablesKeeper.getSensorDevice();
		addListeners();
	}

	private void addListeners() {
		startButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sensorDevice.startPolling(25);
				startButton.setEnabled(false);
				stopButton.setEnabled(true);
				
			}
		});
		
		stopButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sensorDevice.stopPolling();
				stopButton.setEnabled(false);
				startButton.setEnabled(true);
				
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		sensorDevice.startPolling(25);
		if (sensorDevice == null) {
			CharSequence text = "Нет соединения с сервером";
			Toast.makeText(this, text, Toast.LENGTH_LONG).show();
			return;
		}
		while(sensorDevice.get_data() == null){
			try{
				Thread.sleep(200);
				} catch (InterruptedException e){
					;
				}
		}
		if (mChartView == null) {
			LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
			mChartView = chart.execute(this,
					sensorDevice.get_data());
			layout.addView(mChartView, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			layout.setBottom(startButton.getHeight());
			sensorDevice.setDataHandler(this);
			addListeners();
		} else {	
			mChartView.repaint();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, MENU_SETTINGS, 0, R.string.menu_settings).setIcon(
				android.R.drawable.ic_menu_info_details);
		menu.add(Menu.NONE, MENU_DISCONNECT, 1, R.string.menu_disconnect);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case MENU_SETTINGS:
			sensorDevice.setDataHandler(null);
			sensorDevice.stopPolling();
			Intent settingsIntent = new Intent(GraphicsActivity.this,
					SettingsActivity.class);
			GraphicsActivity.this.startActivity(settingsIntent);
			return true;
		case MENU_DISCONNECT:
			sensorDevice.disconnect();
			ThreadLocalVariablesKeeper.clean();
			Intent startIntent = new Intent(GraphicsActivity.this,
					StartActivity.class);
			GraphicsActivity.this.startActivity(startIntent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
		// RelativeLayout buttonsPortraitLayout = (RelativeLayout)
		// findViewById(R.id.portraitGraphicsButtonsLayout);
		// buttonsPortraitLayout.setLayoutParams(new
		// LayoutParams(LayoutParams.MATCH_PARENT, 0));
		// RelativeLayout buttonsLandskapeLayout = (RelativeLayout)
		// findViewById(R.id.landskapeGraphicsButtonsLayout);
		// buttonsLandskapeLayout.setLayoutParams(new
		// LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
		// buttonsLandskapeLayout.setVisibility(View.VISIBLE);
		// LinearLayout mainLayout = (LinearLayout)
		// findViewById(R.id.mainLayout);
		// LinearLayout.LayoutParams params =new
		// LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
		// LayoutParams.MATCH_PARENT);
		// params.bottomMargin = 0;
		// params.rightMargin = (int) convertToPixels(BUTTON_SIDE);
		// mainLayout.addView(mChartView, params);
		//
		// } else {
		// RelativeLayout buttonsPortraitLayout = (RelativeLayout)
		// findViewById(R.id.portraitGraphicsButtonsLayout);
		// buttonsPortraitLayout.setLayoutParams(new
		// LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		// buttonsPortraitLayout.setVisibility(View.VISIBLE);
		// RelativeLayout buttonsLandskapeLayout = (RelativeLayout)
		// findViewById(R.id.landskapeGraphicsButtonsLayout);
		// buttonsLandskapeLayout.setLayoutParams(new LayoutParams(0,
		// LayoutParams.MATCH_PARENT));
		// buttonsLandskapeLayout.setVisibility(View.INVISIBLE);
		//
		// LinearLayout mainLayout = (LinearLayout)
		// findViewById(R.id.mainLayout);
		// LinearLayout.LayoutParams params =new
		// LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
		// LayoutParams.MATCH_PARENT);
		// params.bottomMargin = (int) convertToPixels(BUTTON_SIDE);
		// params.rightMargin = 0;
		// mainLayout.addView(mChartView, params);
		// }

		super.onConfigurationChanged(newConfig);

	}

	private float convertToPixels(float dp) {
		Resources resources = getResources();
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				resources.getDisplayMetrics());

	}

	@Override
	public void processNewData() {
		// TODO Auto-generated method stub
		chart.updateDataSet();
		
	}

}